<template>
	<view class="content">
		<view class="name">
			速推单
		</view>
		<view class="grey">
			请完成微信授权以继续使用
		</view>
		<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			<span class="text">微信登录</span>
		</button>
	</view>
</template>

<script>
	// import {mapMutations} from 'vuex'

	export default {
		data() {
			return {
				check: false,
				userInfo: {
					code: "",
					nickname: "",
					avatar: "",
					encryptedData: "",
					iv: "",
				}
			};
		},
		onShow() {
			const that = this

			uni.login({
				provider: 'weixin', //使用微信登录
				success: function(loginRes) {
					that.userInfo.code = loginRes.code
				}
			});
		},
		methods: {
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户拒绝授权  
					//拒绝授权后弹出一些提示 
					console.log('拒绝授权', e.detail.encryptedData)
				} else { //允许授权  
					this.userInfo.iv = e.detail.iv
					this.userInfo.encryptedData = e.detail.encryptedData
					this.getUserInfo()
				}
			},
			// 用户授权之后获取用户信息
			getUserInfo() {
				const that = this
				uni.getUserInfo({
					desc: '登录的数据',
					success(e) {
						that.userInfo.nickname = e.userInfo.nickName
						that.userInfo.avatar = e.userInfo.avatarUrl
						that.login()
					}
				})
			},
			login() {
				console.log(this.userInfo);
				// const aaa = {
				// 	code: '0d1C15Ha1i2sdG06fmIa1ijODv2C15Hf',
				// 	nickname: ':choco',
				// 	avatar: 'https: //thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
				// 		encryptedData: 'uifr7XmdAKWvtoQtVZbFDpdAWg3RxLzX4/XXsyA52ZyfSJj3l92ITQqM+RpMHJigYrv4DMb0xtU6rz6on4pPt/xtsz7dYbT4yp2geZCdWszXDA0RPQHji18M6dKxK1tpAOgezOlqnr5L0xIfhWiJ9rXWl49H+0wfAbaVT0uwm0WXKJMrc103lKsnU8fCpDmG+RLIZKzitg4a0vY1dl7+2A==',
				// 	iv: 'Gtba5l5FbuSeZttDIJ7hPg=='
				// }
				uni.$http.post('login/register', this.userInfo).then(res => {
					if (res.data.code == 1) {
						uni.setStorageSync('token', res.data.data.token == null ? '' : res.data.data.token)
						uni.setStorageSync('userinfo', res.data.data.token == null ? '' : res.data.data)
						uni.switchTab({
							url: '/pages/my/my'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		min-height: 100vh;
		background-color: #fff;
		padding-top: 139rpx;

		.logo {
			margin-left: 50%;
			transform: translateX(-50%);
			width: 177rpx;
			height: 168rpx;
		}

		.name {
			margin-top: 10rpx;
			font-size: 40rpx;
			font-weight: 600;
			color: #333;
			text-align: center;
			margin-bottom: 100rpx;
		}

		.grey {
			color: #999;
			font-size: 32rpx;
			text-align: center;
		}

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 31rpx auto 50rpx;
			width: 620rpx;
			height: 100rpx;
			line-height: 100rpx;
			background: #2459B9;
			border-radius: 60rpx;
			border: 2rpx solid rgba(5, 5, 5, 0.08);
			color: #fff;

			image {
				margin-right: 10rpx;
				width: 37rpx;
				height: 31rpx;
			}


		}

		.check {
			position: relative;
			display: flex;
			align-items: center;

			image {
				width: 25rpx;
				height: 25rpx;
				position: absolute;
				left: 95rpx;
			}

			.checkText {
				position: absolute;
				left: 130rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}
</style>